/**
 * 
 */
package com.yidu.drug.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.drug.common.Page;
import com.yidu.drug.dao.FinancialSheetsModelMapper;
import com.yidu.drug.dao.RepertoryModelMapper;
import com.yidu.drug.model.FilialeModel;
import com.yidu.drug.model.FinancialSheetsModel;
import com.yidu.drug.model.RepertoryModel;
import com.yidu.drug.service.FilialeService;

/**
 * @author 吴淑侦
 * @date 2018年8月9日 上午8:58:18
 * @version 1.0
 */
@Controller
@RequestMapping("/filiale")
public class FilialeAction {
	@Resource
	private FilialeService filialeService;
	@Resource
	private FinancialSheetsModelMapper financialSheetsDao;
	@Resource
	private RepertoryModelMapper repertoryDao;
	
	/**
	 * 根据条件查询所有
	 * @param page
	 * @param limit
	 * @param filName
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public Map<String, Object> findAll(String page,String limit,String filName,String filSite) throws UnsupportedEncodingException{
		Page pages = new Page();
		pages.setPage(Integer.valueOf(page));
		pages.setRows(Integer.valueOf(limit));
		
		FilialeModel filialeModel = new FilialeModel();
		filialeModel.setFilName(filName);
		filialeModel.setFilSite(filSite);
		filialeModel.setPage(pages);
		
		List<FilialeModel> list = filialeService.findAll(filialeModel);
		for (FilialeModel filialeModel2 : list) {
			//时间转换
			Date time=filialeModel2.getFilDate();
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String tsStr = sdf.format(time.getTime());
	        System.out.println("操作时间"+tsStr);
	        filialeModel2.setTime(tsStr);
		}
		Long count = filialeService.findRows(filialeModel);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", count);
		map.put("data", list);
		return map;	
	}
	
	/**
	 * 查询所有公司
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/findAllFiliale")
	@ResponseBody
	public List<FilialeModel> findAllFiliale(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		List<FilialeModel> list = filialeService.findAllFiliale();
		return list;
	}
	
	/**
	 * 删除(修改状态)
	 * @param filId
	 * @return
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	public String updateById(String filId){
		return filialeService.updateById(filId);
	}
	
	/**
	 * 增加
	 * @param filialeModel
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/add")
	public void add(FilialeModel filialeModel) throws UnsupportedEncodingException{
		String filName=new String(filialeModel.getFilName().getBytes("ISO8859-1"),"UTF-8");
		String filSite=new String(filialeModel.getFilSite().getBytes("ISO8859-1"),"UTF-8");
		String filPreson=new String(filialeModel.getFilPreson().getBytes("ISO8859-1"),"UTF-8");
		String filId = UUID.randomUUID().toString().replaceAll("-", "");
		Timestamp filDate = new Timestamp(new Date().getTime());
		
		filialeModel.setFilId(filId);
		filialeModel.setFilDate(filDate);
		filialeModel.setIsva("有效");
		filialeModel.setFilName(filName);
		filialeModel.setFilSite(filSite);
		filialeModel.setFilPreson(filPreson);
		
		int rows = filialeService.save(filialeModel);
		
		FinancialSheetsModel financialSheetsModel = new FinancialSheetsModel();
		financialSheetsModel.setFsId(UUID.randomUUID().toString().replaceAll("-", ""));
		financialSheetsModel.setFilId(filId);
		BigDecimal bd = new BigDecimal("0");
		financialSheetsModel.setFsPrice(bd);
		financialSheetsModel.setIsva("有效");
		int rowsOne = financialSheetsDao.insertSelective(financialSheetsModel);
		
		RepertoryModel repertoryModel = new RepertoryModel();
		repertoryModel.setRepId(UUID.randomUUID().toString().replaceAll("-", ""));
		repertoryModel.setFilId(filId);
		repertoryModel.setRepNumber((long)0);
		repertoryModel.setIsva("有效");
		int rowTwo = repertoryDao.insertSelective(repertoryModel);
	}
	
	/**
	 * 修改页面
	 * @param request
	 * @param response
	 * @param session
	 * @param filId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	public FilialeModel showUpdate(HttpServletRequest request,HttpServletResponse response,HttpSession session,String filId) throws IOException{
		FilialeModel filialeModel = filialeService.findById(filId);
		session.setAttribute("filId", filId);
		return filialeModel;
	}
	
	/**
	 * 修改
	 * @param filialeModel
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/update")
	public void update(FilialeModel filialeModel) throws UnsupportedEncodingException{
		String filName=new String(filialeModel.getFilName().getBytes("ISO8859-1"),"UTF-8");
		String filSite=new String(filialeModel.getFilSite().getBytes("ISO8859-1"),"UTF-8");
		String filPreson=new String(filialeModel.getFilPreson().getBytes("ISO8859-1"),"UTF-8");
		
		System.out.println(filName+"     "+filSite+"       "+filPreson);
		
		filialeModel.setFilName(filName);
		filialeModel.setFilSite(filSite);
		filialeModel.setFilPreson(filPreson);
		
		Timestamp filDate = new Timestamp(new Date().getTime());
		
		filialeModel.setFilDate(filDate);
		filialeService.update(filialeModel);
		
	}
	
	
}
